﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace carsitem.kucun
{
    public partial class chaxun : Form
    {
        //Sunisoft.IrisSkin.SkinEngine se = null;
        //string SkiName = "Calmness";
        public static string conn = "Data Source=季白\\MSSQLSERVER04;Initial Catalog=SellCars;Integrated Security=True";
        public chaxun()
        {
            InitializeComponent();
            //se = new Sunisoft.IrisSkin.SkinEngine();
            //se.SkinAllForm = true;
            //se.SkinFile = "skin/" + SkiName + ".ssk";
        }

        private void chaxun_Load(object sender, EventArgs e)
        {
            //    //this.label2.Visible = false;
            //    //this.cb_skin.Visible = false;
            //    //1.创建数据库连接对象
            //    SqlConnection con = new SqlConnection("Data Source=季白\\MSSQLSERVER04;Initial Catalog=SellCars;Integrated Security=True");
            //    //con.open();
            //    //2.创建命令对象
            string sql = "select Kucun.*,WarehouseName from kucun join Warehouse on kucun.WarehouseID = Warehouse.WarehouseID";
            NewMethod(sql);
            //    //SqlCommand com = new SqlCommand(sql, con);
            //    //3.执行select查询命令，获取DateAdapter对象
            //    SqlDataAdapter dap = new SqlDataAdapter(sql, con);
            //    //填充DataSet
            //    //DataSet ds = new DataSet();//内存临时仓库
            //    //dap.Fill(ds);//默认填充在ds的第一张表
            //    DataTable dt = new DataTable();//在内存创建了一个表对象
            //    dap.Fill(dt);
            //    //设置不自动生成列
            //    //this.kucunbiao.AutoGenerateColumns = false;
            //    //设置Datagridview控件的数据源
            //    // this.Song_dgv.DataSource = ds.Tables[0];//获取ds的第一张表
            //    this.kucunbiao.DataSource = dt;

        }

        private void chaxun_FormClosed(object sender, FormClosedEventArgs e)
        {
            //Application.Exit();
            carsitem.MainInterface.MainInterfaceForm mainInterfaceForm = new carsitem.MainInterface.MainInterfaceForm();//把form1传给form2
            mainInterfaceForm.Show();
            this.Hide();//把自己隐藏了
        }

        private void cha_btn_Click(object sender, EventArgs e)
        {
            string sql = "select GoodsNumber,StockID,WarehouseName,kucun.WarehouseID,StockNumer from kucun join Warehouse on kucun.WarehouseID = Warehouse.WarehouseID  where 1=1 ";
            //if(this.WarehouseName_txt.Text.Trim() == ""&& this.GoodsNumber_tex.Text.Trim() == "")
            // {
            //     MessageBox.Show("你输入的都是空字符");
            //     return;
            // }
            if (this.WarehouseName_txt.Text.Trim() != "")
            {
                string WarehouseName = this.WarehouseName_txt.Text.Trim();
                sql = sql + string.Format(" and WarehouseName like '%{0}%' ", WarehouseName);
            }
            //string result = System.Text.RegularExpressions.Regex.Replace(this.GoodsNumber_tex.Text.Trim(), @"[^0-9]+", "");
            if (this.GoodsNumber_tex.Text.Trim() != "")
            {
                if (!IsNumber(this.GoodsNumber_tex.Text.Trim()))
                {
                    MessageBox.Show("你的汽车编号输入有问题，请重新输入");
                    return;
                }
                else //if(this.GoodsNumber_tex.Text.Trim() != "")
                {
                    sql = sql + string.Format("  and  GoodsNumber = {0}", this.GoodsNumber_tex.Text.Trim());
                }
            }

            //if (this.GoodsNumber_tex.Text.Trim() != ""&&IsNumber(this.GoodsNumber_tex.Text.Trim()))
            //{

            //    sql = sql + string.Format("  and  GoodsNumber = {0}", this.GoodsNumber_tex.Text.Trim());
            //}
            //else
            //{
            //    MessageBox.Show("你输入有问题，请重新输入");
            //    return;
            //}
            NewMethod(sql);
        }

        private void NewMethod(string sql2)
        {
            //1.创建数据库连接对象
            SqlConnection con = new SqlConnection(conn);
            //con.open();
            //2.创建命令对象
            string sql = string.Format(sql2);
            //SqlCommand com = new SqlCommand(sql, con);
            //3.执行select查询命令，获取DateAdapter对象
            SqlDataAdapter dap = new SqlDataAdapter(sql, con);
            //填充DataSet
            //DataSet ds = new DataSet();//内存临时仓库

            //dap.Fill(ds);//默认填充在ds的第一张表
            DataTable dt = new DataTable();//在内存创建了一个表对象
            dt.Clear();//清除除了条件的数据
            dap.Fill(dt);
            //设置不自动生成列
            this.kucunbiao.AutoGenerateColumns = false;
            //设置Datagridview控件的数据源
            // this.Song_dgv.DataSource = ds.Tables[0];//获取ds的第一张表
            this.kucunbiao.DataSource = dt;
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            string sql = "select GoodsNumber,StockID,WarehouseName,kucun.WarehouseID,StockNumer from kucun join Warehouse on kucun.WarehouseID = Warehouse.WarehouseID  where 1=1 ";
            NewMethod(sql);
        }
        public bool IsNumber(string str_number)
        {
            return System.Text.RegularExpressions.Regex.IsMatch(str_number, "^[0-9]+$");
        }



        //private void cb_skin_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    SkiName = this.cb_skin.Text;
        //    if (SkiName == "默认皮肤")
        //    {
        //        se.Active = false;
        //    }
        //    else
        //    {
        //        se.Active = true;
        //        se.SkinFile = "skin/" + SkiName + ".ssk";
        //    }
        //}
    }
}
